package com.talent.jiwen_teacher.my;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.base.BaseActivity;
import com.talent.jiwen_teacher.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.http.result.MailInfo;
import com.talent.jiwen_teacher.http.result.MailInfoResult;
import com.talent.jiwen_teacher.ui.widgets.NoEmojiEditText;
import com.talent.jiwen_teacher.util.AMUtils;
import com.talent.jiwen_teacher.util.SPUtil;
import com.talent.jiwen_teacher.util.Validators;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;


/**
 * Created by qyqx on 2018/9/21.
 * 填写地址
 */

public class PenAdrActivity extends BaseActivity {

    @BindView(R.id.edit_adr)
    NoEmojiEditText edit_adr;

    @BindView(R.id.adr_phone)
    EditText adr_phone;

    @BindView(R.id.adr_name)
    NoEmojiEditText adr_name;

    @BindView(R.id.submit)
    Button submit;

    String adrStr;
    String nameStr;
    String phoneStr;


    @Override
    protected int provideContentViewId() {
        return R.layout.activity_pen_adr;
    }

    @Override
    public String titleName() {
        return "邮寄地址";
    }

    @Override
    protected void initData() {
        getPenAdr();
    }

    @OnClick({R.id.submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.submit:
                phoneStr=adr_phone.getText().toString().trim();
                adrStr=edit_adr.getText().toString().trim();
                nameStr=adr_name.getText().toString().trim();
                if(!Validators.isEmpty(phoneStr) && !Validators.isEmpty(adrStr) &&  !Validators.isEmpty(nameStr) ){
                    if (phoneStr.length() == 11) {
                        if (AMUtils.isMobile(phoneStr)) {
                            savePenAdr();
                        } else {
                            Toast.makeText(mContext, R.string.Illegal_phone_number, Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(mContext, R.string.Illegal_phone_long, Toast.LENGTH_SHORT).show();
                    }

                }else{
                    showToast("请补全信息");
                }
                break;
        }
    }

    /**保存地址信息
     *
     */
    private void savePenAdr(){
        Map<String , String> map = new HashMap<>();
        map.put("mailAddress" , adrStr);
        map.put("mailReceiverName" , nameStr);
        map.put("mailReceiverPhone" , phoneStr);
        map.put("updateStatus" , ""+updateStatus);
        Observable observable = ApiFactory.getEducationApiSingleton().saveMailInfo(SPUtil.getToken() , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(PenAdrActivity.this) {
            @Override
            protected void _onNext(Void v) {
                showToast("保存成功");
                finish();
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        } , ActivityLifeCycleEvent.DESTROY , lifecycleSubject , false);

    }

    private int updateStatus;//更新状态 0：不更新（保存） 1：更新


    /**获取地址信息
     *
     */
    private void getPenAdr(){
        Observable observable = ApiFactory.getEducationApiSingleton().getTeacherMailInfo(SPUtil.getToken());
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<MailInfoResult>(PenAdrActivity.this) {
            @Override
            protected void _onNext(MailInfoResult mailInfoResult) {
              if(mailInfoResult!=null && mailInfoResult.getMailInfo()!=null){
                  submit.setText("更改");
                  updateStatus=1;
                  MailInfo mailInfo=mailInfoResult.getMailInfo();
                  edit_adr.setText(mailInfo.getReceiveAddress());
                  if(!Validators.isEmpty(mailInfo.getReceiverPhone())){
                      adr_phone.setText(mailInfo.getReceiverPhone());
                  }else{
                      phoneStr= SPUtil.getUserPhone();
                      adr_phone.setText(phoneStr);
                  }

                  if(!Validators.isEmpty(mailInfo.getReceiverName())){
                      adr_name.setText(mailInfo.getReceiverName());
                  }else{
                      nameStr=SPUtil.getUserName();
                      adr_name.setText(nameStr);
                  }
              }else{
                  submit.setText("提交");
                  updateStatus=0;
                  phoneStr= SPUtil.getUserPhone();
                  adr_phone.setText(phoneStr);
                  nameStr=SPUtil.getUserName();
                  adr_name.setText(nameStr);
              }
            }

            @Override
            protected void _onError(String message) {
                showToast(message);

            }
        } , ActivityLifeCycleEvent.DESTROY , lifecycleSubject , true);

    }
}
